package com.yjhb.android.sbz.dialog;

import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yjhb.android.sbz.R;

/**
 * 创建者 zx
 * 创建时间 2020/12/10 11:54
 */
public class InputDialog extends Dialog implements View.OnClickListener {
    private final String TAG = "InputDialog";

    public static final int BUTTON_SUBMIT = 1;
    public static final int BUTTON_CANCEL = 2;

    TextView title_tv;
    TextView tips_tv;
    EditText input_et;
    TextView confirm_tv;
    View close_iv;

    private OnClickListener onClickListener;

    public InputDialog(@NonNull Context context) {
        super(context, R.style.mDialog);
        init();
    }

    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_input, null, false);
        title_tv = view.findViewById(R.id.title_tv);
        tips_tv = view.findViewById(R.id.tips_tv);
        input_et = view.findViewById(R.id.input_et);
        confirm_tv = view.findViewById(R.id.confirm_tv);
        close_iv = view.findViewById(R.id.close_iv);
        close_iv.setOnClickListener(this);
        confirm_tv.setOnClickListener(this);
        setContentView(view);
        setCancelable(true);
        setCanceledOnTouchOutside(false);
    }

    public void setOnClickListener(OnClickListener onClickListener){
        this.onClickListener = onClickListener;
    }

    public EditText getInputView(){
        return input_et;
    }

    public void setConfirmText(CharSequence text){
        this.confirm_tv.setText(text);
    }

    public void setTipsText(CharSequence text){
        this.tips_tv.setText(text);
    }

    public void setTitleText(CharSequence text){
        this.title_tv.setText(text);
    }

    @Override
    public void onClick(View v) {
        if (v == close_iv){
            if (onClickListener!=null){
                onClickListener.onClick(BUTTON_CANCEL);
            }
        }else if (v == confirm_tv){
            if (onClickListener!=null){
                onClickListener.onClick(BUTTON_SUBMIT);
            }
        }
    }

    public interface OnClickListener {
        void onClick(int button);
    }
}
